import CryptoJS from 'crypto-js'
import appConfig from '@/config';

/**
 * 获取url中的参数
 * @params key 需要从url中获取的字段名称
 * @describe 获取url中的参数
 */
export const getQueryString = (key: string) => {
  let { href } = window.location;
  href = href.replace(/#\/.+/, '');
  const src = href.split('?');
  if (src[0] === href) {
    return '';
  }
  const arr = src[1].split('&');
  const obj: any = {};
  for (let i = 0; i < arr.length; i++) {
    const arr1 = arr[i].split('=');
    obj[arr1[0]] = arr1[1];
  }
  return (obj[key] || '').replace(/(\/|#).?/g, '');
};

/**
 * 将json对象转换为 FormData类型
 * @param data 【参数是json对象】
 * @return [返回 FormData类型 的值]
 */
export const transformDataToFormData = (data: any) => {
  const form = new FormData();
  Object.keys(data).forEach((key) => {
    if (data[key] instanceof Array) {
      data[key].forEach((value: any) => {
        form.append(key, value);
      });
    } else {
      form.append(key, data[key]);
    }
  });

  return form;
};

/**
 * 下载文件
 * @param data 【数据流】
 * @param fileName 【文件名】
 * @param type 【文件 MIME 类型】
 */
export const downloadFile = (data: any, fileName: string, type: string) => {
  const link = document.createElement('a');
  const blob = new Blob([data], {
    type,
  });

  link.style.display = 'none';
  link.href = URL.createObjectURL(blob); // 创建url对象
  link.download = fileName; // 下载后文件名
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(link.href); // 销毁url对象
};

/**
 * SH256加密
 * @param data 【需要加密的数据】
 * @return [返回加密后的数据]
 */
export const encryptSh256 = (obj: object = {}) => {
  const dataJson = JSON.stringify(obj.data).replaceAll(" ", "");
  const secret = appConfig.sha256Key;
  const hash = CryptoJS.SHA256(dataJson + obj.timestamp + obj.nonce + secret);
  // console.log("dataJson = ", dataJson);
  // console.log("timestamp = ", obj.timestamp);
  // console.log("nonce = ", obj.nonce);
  // console.log("secret = ", secret);
  // console.log("hash = ", hash.toString());
  return hash.toString();
}
/**
 * SHA256加密测试
 * @example 示例
 * timestamp: 1725277079000
 * nonce: 1725277079
 * secret: aa8f9c5a6f714dd2ab90f094750c2039
 * 参与字符串：{"name":"lizhiwei","age":18}17252770790001725277079aa8f9c5a6f714dd2ab90f094750c2039
 * 最终签名结果: 36796532d75e5c7be1438c1908f45fea967f44f5a0c338e0bcf1b7ba0f997958
 */
export const encryptSh256Test = () => {
  const dataJson = `{"name":"lizhiwei","age":18}`;
  const secret = appConfig.sha256Key;
  const hash = CryptoJS.SHA256(dataJson + 1725277079000 + 1725277079 + secret);
    return hash.toString();
}